This article will give you the overview about web technologies along with the Struts 2 framework. It will also cover the details about MVC architecture followed by the architecture of the Struts 2 framework. It's also important to go through the major differences between Struts 1.x and Struts 2.x which will be followed by the application development process of the Struts 2.

Table of content:

 

1. Overview of MVC Architecture

Today's world is 'online world' where people are highly dependent on the internet and web applications. Millions of web applications and thousands of technologies are available in the market for enterprise application development. All of these web technologies are categorized as Model1, Model2, MVC and much more. Struts is MVC Model2 architecture based framework.

MVC means Model-View-Controller where model is responsible for business logic, view handles the user interaction with application and controller decides the flow of application.

Figure 1.1 explains the working methodology behind MVC architecture which is as follows:
1) User sends the request using view which reaches to the controller on the server.
2) Controller decides the respective model and delivers the control to particular model.
3) As a result of business processing, model changes its state.
4) Depending on the state change of model, control redirects the flow to the view.
Main advantage of this architecture is the separation of data layer, presentation layer and pure business logic.

MVC Architecture

Figure 1.1 MVC architecture

2. What is Struts?

Struts is a framework based on set of Java technologies like Servlet, JSP, JSTL, XML etc which provides implementation of MVC architecture. The framework also provides ready to use validation framework. The power of Struts lies in its model layer by which Struts can be integrated with other Java technologies like JDBC, EJB, Spring, Hibernate and many more.

Struts is an open source framework developed by Apache Software foundation. It can be downloaded as a free source from apache website. Struts can be broadly classified as Struts 1.X and Struts 2.X. Though Struts 2.X is successor of Struts 1.X, there is a vast difference between them. Struts 2.x is based on WebWorks2 framework. Struts 2.x is also called as pull-MVC architecture because data that needs to be displayed  to the user can be pulled from the Action(a class which contains business logic).

ActionServlet is the name of a class that plays role of controller in Struts. Whenever the user sends a request to server it passes via ActionServlet which then decides the necessary model and sends this request to the respective model. Two more files support the flow of application: web.xml and struts-config.xml files. Web.xml file is deployment descriptor which keeps all the application related settings while struts-config.xml file maps a request to Action classes and ActionForms(a simple POJO which contains properties related to UI). Model is handled by various Java technologies like EJB, JDBC, Hibernate, Spring etc. It mainly concentrates on the business logic and semantics of the application. While view can be developed using JSP, Velocity Templates, JSTL, OGNL, XSLT and other HTML technologies. View is responsible for getting the input from the user and rendering the result of that input sent back to user.

 

3. Architecture of Struts 2 Framework

Let's discuss Struts 2 architecture diagram. Struts 2 architecture is divided in 4 major categories.

1) Servlet filters
2) Struts core
3) Interceptors
4) User defined classes/files.

2.1 Struts 2 Architecture

Figure 2.1 Struts 2 Architecture

 

1) Servlet filters -Servlet filter comprises of 3 major components, those are ActionContextCleanUp, SiteMesh and FilterDispatcher.
When a user sends a request from browser to web server, the request reaches to ServletContainer. The request passes through the sequence of filters and cleanup classes. ActionContextCleanup is used when we integrate Struts with other Java technologies but it is an optional filter. SiteMesh is a framework helps maintaining consistent look & feel, navigations and layout across pages. It is also an optional filter. The next is FilterDispatcher. It is a required filter and used as a link between filters and ActionMapper(ActionMapper is the part of Struts Core). FilterDispatcher is also known as a bridge between Servlet Filters and Struts Core. FilterDispatcher handles Action execution, cleanup of context and can also provide static content of the application.

2) Struts Core : This is the actual Model part of Struts 2. It contains the main business logic. Struts Core category consists of Action, Interceptor,ActionProxy, ActionMapper, Result and Tag library systems.

Here, FilterDispatcher invokes ActionMapper to check whether incoming request is mapped to any action or interceptor or nothing. ActionMapper returns NULL if no Action invocation matches. ActionMapper can also return multiple Interceptors at a time. In this case, each interceptor is called one after the other. Here Interceptor is responsible for workflow implementation, avoids double submission problem, logging, validation, file upload etc.

Interceptor is the most useful feature of the Struts2.

Then ActionMapper decides which action to be called and returns the same to the FilterDispatcher. Now FilterDispatcher delegates the control to the ActionProxy. ActionProxy uses ConfigurationFile manager and creates an instance of ActionInvocation which will instantiate Action class. ActionInvocation will take help of struts.xml file to determine which method of the Action class should be called. ActionProxy can also call Interceptors with the help of ActionInvocation.

Now actual Action is executed and state of the model is changed as per the user input. After successful execution of Action class one Result object is created which will be mapped to the UI (JSP, JS, Struts2 Tags, Velocity or any other template). struts.xml plays an important role in this mapping. Data is populated in above mentioned frontend technologies from this Result object and final response is created which will be sent to the client browser. If there are any interceptors on the way, they will be executed in reverse order. Tag systems are used to improve views which include HTML, DOJO framework, etc.

 

3) Interceptors: In Struts 2, almost every action is associated with an interceptor or a set of interceptors. It can be called before or after the request processing. It can also be configured for each action basis individually. Interceptors define common cross cutting tasks in clean and reusable architecture.

VALUESTACK is a storage area which is used to store data associated with request processing. OGNL (Object Graph Navigation Language) provides the functionality of retrieving data stored in VALUESTACK. VALUESTACK is actually a stack which stores following objects in respective order.

a) Temporary Objects - These are the objects which are created during execution of action and placed onto the VALUESTACK. For e.g. various flags or local variables used in a JSP.

b) Model Objects - Current model object is placed on the VALUESTACK before the action class is executed.

c) Action Objects - Action objects are the actions which are being executed.

d) Named Objects - Application, Session, Request, Response etc are called as Named objects. These objects define the scope of variables.

4) User defined classes/files - Action classes, JSP or any other view and struts.xml etc are part of user defined classes or files. Action classes change the state of the model as per business requirement and return this state to the respective JSP or any other view with the help of struts.xml file. Application developer has to concentrate on user defined classes or files only. Rest all the components are taken care by the framework.

 

4. Features of Struts 2

1) Intelligent defaults in all configuration files: One of the useful features of Struts 2 is their default values. Hence users do not need to change or set the obvious default values in configuration files.

2) Use of OGNL: Struts 2 uses elegant expression language called OGNL which is more powerful and flexible then JSTL. OGNL stands for Object Graph Navigation Language. It supports AJAX implementation in UI. Struts 2 also supports AJAX enabled tags.

3) Ready to use view components: Struts2 tags provide style sheet driven form tags which reduce coding efforts and coding of form validations.

4) Strong validation support: Struts2 supports Xwork validation framework which provides client side and server side validation. It provides manual validation as well with the support of Validate() method but most of the time developer needn't go for the manual validation.

5) Type conversion mechanism: Type conversion in Struts 2 is supported by OGNL. In Struts 1 all the properties are mostly of String type but in Struts 2 these properties can be of any type.

6) Decoupling of view from Action classes:  In Struts 2, one view can be associated with multiple action classes.

7) Simple Implementation: Action classes are not tightly coupled with ServletContainer. In addition to this, Action class need not have ‘execute’ method implemented in it. There are very less dependency on Struts core library. Action class can be a simple POJO. Similar to action classes, ActionForm can also be a simple POJO.

 

5. Comparison of Struts 1.x and Struts 2.x

Though Struts 2.x is successor of Struts 1.x, there is a vast difference between both of them. Both architectures are completely different from each other.

1) Action class: Struts 1 action class must extend Struts core and abstract class Action while Struts 2 action classes are POJOs. Struts 1 Action class must override execute method but there is no such compulsion in Struts 2 Action class. In Struts 2, Any method of the given Action class can be called from the ActionProxy object. In addition to this, execute method of Struts 1 accepts four parameters i.e. HttpServletRequest, HttpServletResponse, ActionMapping and ActionForward. Hence to test an Action class you have to have HttpServletRequest and HttpServletResponse objects but in Struts 2 Action can be tested standalone.

2)  ActionForm:  Similar to Action classes, ActionForms in Struts 2 is also simple POJO. In Struts 1 ActionForm has to extend base class but in Struts 2 it is not required. In Struts 1, all the properties of the class are of type string but in Struts 2 they can be of any type. 

3)  Validation: Struts 2 has enhanced automatic validation as compared to Struts 1. 

4)  Type conversion: Struts1 uses common-beanutils for type conversion while Struts 2 uses OGNL for type conversion. 

5)  Servlet Coupling:  Struts 1 is tightly coupled with Servlet API and browser request passes through the ServletContainer hence it cannot be tested easily. Whereas Struts 2 is not coupled with Servlet API hence Struts 2 request can easily be tested compared to Struts 1.

6) Thread-Safe: Struts 1 class is singleton and is not thread safe hence developer has to be cautious while writing Action class. In Struts 2, a new Action class is instantiated for every request which makes it Thread-Safe.

7) Transfer of Input data from view to model: Struts 1 is using ActionForm beans to capture input data provided by the user. Struts 2 uses properties in Action class itself to capture input so there’s no need of a separate Java class.

8) Expression Language: Struts 1 uses JSTL as an expression language while Struts 2 uses elegant and more powerful expression language OGNL which can reduce the code up to 30% to 40%

9) Dynamic values in View : Struts 1 uses JSP mechanism to store dynamic values entered on views while Struts 2 uses ValueStack storage to store Appplication, Session and Request objects. ValueStack can also store temporary local objects during execution of the cycle.

10) Interceptors: In Struts 1, there is no provision to check the request before Action class' execution while in Struts 2 there is a concept called Interceptors which checks and validates the request before reaching to Action class.

11) Naming Convention: struts-config.xml of Struts 1 is similar to struts.xml of Struts 2, RequestProcessor is similar to Interceptor, ActionForward is similar to Result, ActionMapping is similar to Package and action, ActionServlet is similar to FilterDispatcher. Just naming convention is different. In Struts 2, struts.xml contains tag called Package. By using package actions can be divided in various categories as per domain.

12) Annotations:  Struts 1 is based on Java 1.4 so generally it does not use Annotations in configuration files while Struts 2 is based on Java 5 so it uses Annotations in configuration files which simplifies the structure of configuration file.

13) Dependency Injection: Struts 2 supports dependency Injection. It is the process of embedding an external dependency into the application component. This is a special form of Inversion of Control which is not supported in Struts 1.

14) Result types: Struts 1 supports only one result type that is JSP while Struts 2 supports many other result types like JSP, XML, Velocity, FreeMarker etc.

 

6. Comparison with Spring and Hibernate

    • Struts is used to develop only frontend framework while Hibernate is used to develop backend systems. In contrast to above two, Spring is complete and modular framework i.e. it works on front end as well as backend development. So technically you can say Struts and Hibernate are subset of Spring framework.
    • Struts uses Action class to write business logic while Spring uses Controller classes. Hibernate DAO classes contains logic related to the persistence of object.
    • Action class in Struts is an abstract class while Controller class in Spring is Interface so performance vice Controller class of Spring is better than Action class of Struts. The main reason is a java class can only extend one class, but at the same time it can implement any number of interfaces, hence it is better to use interface rather than using abstract class.
    • ActionForward in struts is replaced by ModelAndView object in Spring.
    • Struts implements MVC architecture. Hibernate is ORM based framework. Spring framework is very modular and has multiple modules. Spring MVC module is based on MVC design pattern.  It supports IOC, AOP, ORM etc.
    • Main advantages of Spring framework is Transaction Management and Messaging support. In addition to this Spring very powerful with support of IOC, AOP, MVC, ORM and much more. Also Spring can be easily  integrated with other frameworks like struts, hibernate etc. The  advantages of Struts framework is  elegant tag library support and easy integration with other front end technologies. The main advantage  of Hibernate framework is database independency because Hibernate uses HQL to develop database independent queries.

7. Advantages of Struts2

1) Simplified Design: Code is not tightly coupled to Struts framework or Servlet API.

2) Easy plug-in: Developers can use other technologies plug-in easily. It includes SiteMesh, Spring, Tiles, etc.

3) Simplified ActionForm: ActionForms are POJOs, we do not need to implement any interface or extend from any class.

4) Annotations introduced: Use of annotation results in reduction in length and complexity of code. It is also used in configuration file for simplicity.

5) Better tag features: It includes theme based tags and Ajax enabled tags.

6) Simplified Testability: Unit testing of Struts 2 Action class is very easy because it doesn’t need complex HttpServletRequest and HttpServletResponse objects.

7) Simplified Action: Similar to ActionForms, Actions are also simple POJOs and they do not need to implement any interface or extend any class.

8) OGNL integration: It uses OGNL to fetch data from ValueStack and type conversion which reduces code.

9) Ajax support: Struts2 tags are Ajax enabled.

10) Multiple View options: View is not restricted to JSP. Freemarker, velocity templates can also be used as a view.

 

8. Disadvantages of Struts2

1) Compatibility: Struts 2 is completely different from the Struts 1. So it’s difficult to perform migration of applications from Struts 1 to Struts 2.

2) Limited Documentation: Limited documentation is available for Struts 2. In addition to this, new users find it difficultly to understand its concepts due to poorly managed documentation by Apache.

 

9. Struts 2 plugin

Struts 2 plugin is used to extend the functionality of Struts 2 framework. Struts 2 plugin is nothing but a simple JAR file or collection of JAR files which provides some predefined additional functionality to framework. Main advantage of plugin is it’s very easy to integrate it with existing Struts applications as they are pluggable. To use the plugin, developer has to add JAR file in the class path of the application. In today’s world there are lots of struts plugins available in the market. Since plugin is a JAR file it can be easily shared with other developers. Hence it satisfies JAVA’s main characteristic “Develop once use anywhere”. Plugins are also used to integrate other Java Technologies with Struts 2.

Let’s take a quick look at the examples of Struts 2 plugin.

1) JQuery plugin: One of the most famous plugin of Struts 2 framework is JQuery plugin. It provides ajax functionality and UI Widgets based on the jQuery javascript framework. It includes many ready to use UI controlsDatePicker, Dialog, Tabbed Panel, Auto completer, Accordion, Charts, Tree, Spinner, Grid, Rich Text Editor, ProgressBar etc.

2) JSF plugin: It provides support for Java Server Faces components with no additional configurations.

3) JUnit plugin: It provides support for JUnit classes to perform the unit testing. JUnit is very well known unit testing framework.

4) Image plugin: This plugin is deprecated now but it provides all the functionality to improve the image or to perform any kind of image processing.

5) SiteMesh plugin: This plugin is now used as a replacement of Tiles framework. It provides the common UI in application. It is mostly used to develop header, common menu and footer functionality.

6) Portlet plugin: It is used to develop JSR 168 portlet using Struts 2.

7) Spring plugin: allows Actions, Interceptors, and Results to be created and/or autowired by Spring

8) Full Hibernate Plugin: The Full Hibernate Plugin provides Hibernate Validator integration, Hibernate Core Sessions and Transactions injection capabilities and a Configuration Management Web Tool. Many developers use Spring Framework to configure Hibernate Core Sessions and Transactions management. This plugin makes it very easy in Struts 2.

In addition to this there are many other Struts plugins like Config Browser plugin, Convention plugin, JasperReport plugin, DWR plugin, Java templates plugin, JSON plugin, OVal plugin, Embedded JSP plugin,  Pell multipart login, Tiles plugin, Struts 1 plugin, SiteGraph plugin, SmartURLs plugin, TestNG plugin. There are many deprecated plugins which do not need to discuss here.

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment